namespace KPIS.GERP.Models.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class update0005 : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.IcmMasPurchaseRegularMapping",
                c => new
                    {
                        RegularMapId = c.Int(nullable: false, identity: true),
                        RegularId = c.Int(nullable: false),
                        BgSourceId = c.Int(nullable: false),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.RegularMapId)
                .ForeignKey("dbo.IcmMasPurchaseRegular", t => t.RegularId, cascadeDelete: true)
                .ForeignKey("dbo.bgm_mas_source", t => t.BgSourceId, cascadeDelete: true)
                .Index(t => t.RegularId)
                .Index(t => t.BgSourceId);
            
            CreateTable(
                "dbo.IcmMasContactBank",
                c => new
                    {
                        ConBankId = c.Int(nullable: false, identity: true),
                        ConId = c.Int(nullable: false),
                        BranchId = c.Int(nullable: false),
                        AccountCode = c.String(nullable: false),
                        AccountName = c.String(nullable: false),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.ConBankId)
                .ForeignKey("dbo.IcmMasContact", t => t.ConId, cascadeDelete: true)
                .ForeignKey("dbo.FimMasBankBranch", t => t.BranchId, cascadeDelete: true)
                .Index(t => t.ConId)
                .Index(t => t.BranchId);
            
            CreateTable(
                "dbo.IcmMasContactPartner",
                c => new
                    {
                        ConPartId = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                        Descr = c.String(),
                        ConId = c.Int(nullable: false),
                        CitizenId = c.String(nullable: false, maxLength: 13),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.ConPartId)
                .ForeignKey("dbo.IcmMasContact", t => t.ConId, cascadeDelete: true)
                .Index(t => t.ConId);
            
            CreateTable(
                "dbo.IcmReceive",
                c => new
                    {
                        ReceiveId = c.Int(nullable: false, identity: true),
                        ReceiveCode = c.String(),
                        ReceiveName = c.String(nullable: false),
                        ReceiveTypeId = c.Int(nullable: false),
                        ReceiveDate = c.DateTime(nullable: false, storeType: "datetime2"),
                        DnId = c.Int(),
                        WhId = c.Int(nullable: false),
                        RefNo = c.String(),
                        Remark = c.String(),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(nullable: false),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.ReceiveId)
                .ForeignKey("dbo.IcmWarehouse", t => t.WhId, cascadeDelete: true)
                .ForeignKey("dbo.IcmDeliveryNote", t => t.DnId)
                //.ForeignKey("dbo.IcmMasLookUp", t => t.ReceiveTypeId, cascadeDelete: true)
                .Index(t => t.WhId)
                .Index(t => t.DnId)
                .Index(t => t.ReceiveTypeId);
            
            CreateTable(
                "dbo.IcmWarehousePermit",
                c => new
                    {
                        WarehousePermitId = c.Int(nullable: false, identity: true),
                        WhId = c.Int(nullable: false),
                        JobSiteId = c.Int(nullable: false),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.WarehousePermitId)
                .ForeignKey("dbo.IcmWarehouse", t => t.WhId, cascadeDelete: true)
                .ForeignKey("dbo.hrm_job_site", t => t.JobSiteId, cascadeDelete: true)
                .Index(t => t.WhId)
                .Index(t => t.JobSiteId);
            
            CreateTable(
                "dbo.IcmReceiveDtl",
                c => new
                    {
                        ReceiveDtlId = c.Int(nullable: false, identity: true),
                        ReceiveId = c.Int(),
                        DeliveryNoteItemId = c.Int(nullable: false),
                        IsByPassInventory = c.Boolean(),
                        ResourceItemId = c.Int(),
                        ResourceTypeId = c.Int(),
                        UnitId = c.Int(),
                        DepreciationTypeId = c.Int(),
                        ProjExpId = c.Int(),
                        Quantity = c.Int(nullable: false),
                        Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                        Remark = c.String(),
                        Description = c.String(),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(nullable: false),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.ReceiveDtlId)
                .ForeignKey("dbo.IcmReceive", t => t.ReceiveId)
                .ForeignKey("dbo.IcmMasResourceItem", t => t.ResourceItemId)
                .ForeignKey("dbo.IcmMasResourceType", t => t.ResourceTypeId)
                .ForeignKey("dbo.IcmMasUnit", t => t.UnitId)
                .ForeignKey("dbo.bgm_project_expense", t => t.ProjExpId)
                .ForeignKey("dbo.IcmMasDepreciationType", t => t.DepreciationTypeId)
                .Index(t => t.ReceiveId)
                .Index(t => t.ResourceItemId)
                .Index(t => t.ResourceTypeId)
                .Index(t => t.UnitId)
                .Index(t => t.ProjExpId)
                .Index(t => t.DepreciationTypeId);
            
            CreateTable(
                "dbo.IcmAsset",
                c => new
                    {
                        AssetId = c.Int(nullable: false, identity: true),
                        AssetCode = c.String(nullable: false, maxLength: 23),
                        ResItemId = c.Int(nullable: false),
                        AssetName = c.String(nullable: false),
                        AssetDate = c.DateTime(nullable: false),
                        SerialNumber = c.String(nullable: false, maxLength: 50),
                        IsLowerAsset = c.Boolean(),
                        Quantity = c.Int(nullable: false),
                        Price = c.Double(nullable: false),
                        WhId = c.Int(nullable: false),
                        UnitId = c.Int(nullable: false),
                        AssetTypeId = c.Int(),
                        QualityAssuranceExpireDate = c.DateTime(),
                        ProjExpId = c.Int(),
                        ReceiveDtlId = c.Int(nullable: false),
                        ResSubGrpId = c.Int(nullable: false),
                        ResGrpId = c.Int(nullable: false),
                        AssetStatusId = c.Int(nullable: false),
                        DepreciationTypeId = c.Int(),
                        JobSiteId = c.Int(),
                        Description = c.String(),
                        PicturePath = c.String(),
                        Place = c.String(),
                        Remark = c.String(),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(nullable: false),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.AssetId)
                .ForeignKey("dbo.IcmReceiveDtl", t => t.ReceiveDtlId, cascadeDelete: true)
                .ForeignKey("dbo.bgm_project_expense", t => t.ProjExpId)
                .ForeignKey("dbo.IcmMasResourceItem", t => t.ResItemId, cascadeDelete: true)
                .ForeignKey("dbo.IcmMasResourceType", t => t.AssetTypeId)
                .ForeignKey("dbo.IcmWarehouse", t => t.WhId, cascadeDelete: true)
                .ForeignKey("dbo.IcmMasUnit", t => t.UnitId, cascadeDelete: true)
                .ForeignKey("dbo.IcmMasResourceSubGroup", t => t.ResSubGrpId, cascadeDelete: true)
                .ForeignKey("dbo.IcmMasResourceGroup", t => t.ResGrpId, cascadeDelete: true)
                //.ForeignKey("dbo.IcmMasLookUp", t => t.AssetStatusId, cascadeDelete: true)
                .ForeignKey("dbo.hrm_job_site", t => t.JobSiteId)
                .ForeignKey("dbo.IcmMasDepreciationType", t => t.DepreciationTypeId)
                .Index(t => t.ReceiveDtlId)
                .Index(t => t.ProjExpId)
                .Index(t => t.ResItemId)
                .Index(t => t.AssetTypeId)
                .Index(t => t.WhId)
                .Index(t => t.UnitId)
                .Index(t => t.ResSubGrpId)
                .Index(t => t.ResGrpId)
                .Index(t => t.AssetStatusId)
                .Index(t => t.JobSiteId)
                .Index(t => t.DepreciationTypeId);
            
            CreateTable(
                "dbo.IcmAssetWarranty",
                c => new
                    {
                        WarrantyId = c.Int(nullable: false, identity: true),
                        AssetId = c.Int(nullable: false),
                        WarrantyNo = c.String(nullable: false, maxLength: 50),
                        WarrantyPrice = c.Decimal(nullable: false, precision: 18, scale: 2),
                        WarrantyTime = c.Int(nullable: false),
                        WarrantyStartDate = c.DateTime(nullable: false),
                        WarrantyExpireDate = c.DateTime(nullable: false),
                        Description = c.String(maxLength: 255),
                        Remark = c.String(),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(nullable: false),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.WarrantyId)
                .ForeignKey("dbo.IcmAsset", t => t.AssetId, cascadeDelete: true)
                .Index(t => t.AssetId);
            
            CreateTable(
                "dbo.IcmOfficeSuppliesLot",
                c => new
                    {
                        OfficeSuppliesLotId = c.Int(nullable: false, identity: true),
                        ResItemId = c.Int(),
                        LotNo = c.String(nullable: false, maxLength: 50),
                        Description = c.String(nullable: false),
                        Quantity = c.Int(nullable: false),
                        Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                        WhId = c.Int(),
                        ProjExpId = c.Int(),
                        ReceiveDtlId = c.Int(),
                        UnitId = c.Int(),
                        ExpireDate = c.DateTime(),
                        PicturePath = c.String(),
                        Remark = c.String(),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(nullable: false),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.OfficeSuppliesLotId)
                .ForeignKey("dbo.IcmReceiveDtl", t => t.ReceiveDtlId)
                .ForeignKey("dbo.IcmWarehouse", t => t.WhId)
                .ForeignKey("dbo.IcmMasUnit", t => t.UnitId)
                .ForeignKey("dbo.bgm_project_expense", t => t.ProjExpId)
                .ForeignKey("dbo.IcmMasResourceItem", t => t.ResItemId)
                .Index(t => t.ReceiveDtlId)
                .Index(t => t.WhId)
                .Index(t => t.UnitId)
                .Index(t => t.ProjExpId)
                .Index(t => t.ResItemId);
            
            CreateTable(
                "dbo.IcmRequestParcel",
                c => new
                    {
                        ReqParcelId = c.Int(nullable: false, identity: true),
                        IcmReqCode = c.String(nullable: false),
                        IcmResItemCode = c.String(),
                        JobSiteId = c.Int(),
                        RequestName = c.String(nullable: false),
                        WarehousePermitId = c.Int(nullable: false),
                        Remarks = c.String(),
                        RequestBy = c.Int(nullable: false),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                        IcmResTypeId = c.Int(),
                        WantedDate = c.DateTime(nullable: false),
                        ApprovedBy = c.Int(),
                        ApprovedDate = c.DateTime(),
                        Reason = c.String(),
                        RecordStatus = c.String(maxLength: 2),
                        ProjectId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.ReqParcelId)
                .ForeignKey("dbo.hrm_job_site", t => t.JobSiteId)
                .ForeignKey("dbo.hrm_ps_employees", t => t.RequestBy, cascadeDelete: true)
                .ForeignKey("dbo.IcmWarehousePermit", t => t.WarehousePermitId, cascadeDelete: true)
                .ForeignKey("dbo.IcmMasResourceType", t => t.IcmResTypeId)
                .ForeignKey("dbo.bgm_project", t => t.ProjectId, cascadeDelete: true)
                .Index(t => t.JobSiteId)
                .Index(t => t.RequestBy)
                .Index(t => t.WarehousePermitId)
                .Index(t => t.IcmResTypeId)
                .Index(t => t.ProjectId);
            
            CreateTable(
                "dbo.IcmRequestParcelDtl",
                c => new
                    {
                        ReqDtlId = c.Int(nullable: false, identity: true),
                        ReqId = c.Int(),
                        ResItemId = c.Int(),
                        PprId = c.Int(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                        Quantity = c.Decimal(precision: 18, scale: 2),
                        ItemName = c.String(),
                        RequestType = c.String(),
                        RequestTypeCode = c.String(),
                        JobsiteId = c.Int(nullable: false),
                        RecordStatus = c.String(maxLength: 2),
                        DispenseStatus = c.String(maxLength: 10),
                    })
                .PrimaryKey(t => t.ReqDtlId)
                .ForeignKey("dbo.IcmRequestParcel", t => t.ReqId)
                .ForeignKey("dbo.IcmMasResourceItem", t => t.ResItemId)
                .ForeignKey("dbo.IcmPrePurchaseRequest", t => t.PprId)
                .ForeignKey("dbo.hrm_job_site", t => t.JobsiteId, cascadeDelete: true)
                .Index(t => t.ReqId)
                .Index(t => t.ResItemId)
                .Index(t => t.PprId)
                .Index(t => t.JobsiteId);
            
        }
        
        public override void Down()
        {
            DropIndex("dbo.IcmRequestParcelDtl", new[] { "JobsiteId" });
            DropIndex("dbo.IcmRequestParcelDtl", new[] { "PprId" });
            DropIndex("dbo.IcmRequestParcelDtl", new[] { "ResItemId" });
            DropIndex("dbo.IcmRequestParcelDtl", new[] { "ReqId" });
            DropIndex("dbo.IcmRequestParcel", new[] { "ProjectId" });
            DropIndex("dbo.IcmRequestParcel", new[] { "IcmResTypeId" });
            DropIndex("dbo.IcmRequestParcel", new[] { "WarehousePermitId" });
            DropIndex("dbo.IcmRequestParcel", new[] { "RequestBy" });
            DropIndex("dbo.IcmRequestParcel", new[] { "JobSiteId" });
            DropIndex("dbo.IcmOfficeSuppliesLot", new[] { "ResItemId" });
            DropIndex("dbo.IcmOfficeSuppliesLot", new[] { "ProjExpId" });
            DropIndex("dbo.IcmOfficeSuppliesLot", new[] { "UnitId" });
            DropIndex("dbo.IcmOfficeSuppliesLot", new[] { "WhId" });
            DropIndex("dbo.IcmOfficeSuppliesLot", new[] { "ReceiveDtlId" });
            DropIndex("dbo.IcmAssetWarranty", new[] { "AssetId" });
            DropIndex("dbo.IcmAsset", new[] { "DepreciationTypeId" });
            DropIndex("dbo.IcmAsset", new[] { "JobSiteId" });
            DropIndex("dbo.IcmAsset", new[] { "AssetStatusId" });
            DropIndex("dbo.IcmAsset", new[] { "ResGrpId" });
            DropIndex("dbo.IcmAsset", new[] { "ResSubGrpId" });
            DropIndex("dbo.IcmAsset", new[] { "UnitId" });
            DropIndex("dbo.IcmAsset", new[] { "WhId" });
            DropIndex("dbo.IcmAsset", new[] { "AssetTypeId" });
            DropIndex("dbo.IcmAsset", new[] { "ResItemId" });
            DropIndex("dbo.IcmAsset", new[] { "ProjExpId" });
            DropIndex("dbo.IcmAsset", new[] { "ReceiveDtlId" });
            DropIndex("dbo.IcmReceiveDtl", new[] { "DepreciationTypeId" });
            DropIndex("dbo.IcmReceiveDtl", new[] { "ProjExpId" });
            DropIndex("dbo.IcmReceiveDtl", new[] { "UnitId" });
            DropIndex("dbo.IcmReceiveDtl", new[] { "ResourceTypeId" });
            DropIndex("dbo.IcmReceiveDtl", new[] { "ResourceItemId" });
            DropIndex("dbo.IcmReceiveDtl", new[] { "ReceiveId" });
            DropIndex("dbo.IcmWarehousePermit", new[] { "JobSiteId" });
            DropIndex("dbo.IcmWarehousePermit", new[] { "WhId" });
            DropIndex("dbo.IcmReceive", new[] { "ReceiveTypeId" });
            DropIndex("dbo.IcmReceive", new[] { "DnId" });
            DropIndex("dbo.IcmReceive", new[] { "WhId" });
            DropIndex("dbo.IcmMasContactPartner", new[] { "ConId" });
            DropIndex("dbo.IcmMasContactBank", new[] { "BranchId" });
            DropIndex("dbo.IcmMasContactBank", new[] { "ConId" });
            DropIndex("dbo.IcmMasPurchaseRegularMapping", new[] { "BgSourceId" });
            DropIndex("dbo.IcmMasPurchaseRegularMapping", new[] { "RegularId" });
            DropForeignKey("dbo.IcmRequestParcelDtl", "JobsiteId", "dbo.hrm_job_site");
            DropForeignKey("dbo.IcmRequestParcelDtl", "PprId", "dbo.IcmPrePurchaseRequest");
            DropForeignKey("dbo.IcmRequestParcelDtl", "ResItemId", "dbo.IcmMasResourceItem");
            DropForeignKey("dbo.IcmRequestParcelDtl", "ReqId", "dbo.IcmRequestParcel");
            DropForeignKey("dbo.IcmRequestParcel", "ProjectId", "dbo.bgm_project");
            DropForeignKey("dbo.IcmRequestParcel", "IcmResTypeId", "dbo.IcmMasResourceType");
            DropForeignKey("dbo.IcmRequestParcel", "WarehousePermitId", "dbo.IcmWarehousePermit");
            DropForeignKey("dbo.IcmRequestParcel", "RequestBy", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.IcmRequestParcel", "JobSiteId", "dbo.hrm_job_site");
            DropForeignKey("dbo.IcmOfficeSuppliesLot", "ResItemId", "dbo.IcmMasResourceItem");
            DropForeignKey("dbo.IcmOfficeSuppliesLot", "ProjExpId", "dbo.bgm_project_expense");
            DropForeignKey("dbo.IcmOfficeSuppliesLot", "UnitId", "dbo.IcmMasUnit");
            DropForeignKey("dbo.IcmOfficeSuppliesLot", "WhId", "dbo.IcmWarehouse");
            DropForeignKey("dbo.IcmOfficeSuppliesLot", "ReceiveDtlId", "dbo.IcmReceiveDtl");
            DropForeignKey("dbo.IcmAssetWarranty", "AssetId", "dbo.IcmAsset");
            DropForeignKey("dbo.IcmAsset", "DepreciationTypeId", "dbo.IcmMasDepreciationType");
            DropForeignKey("dbo.IcmAsset", "JobSiteId", "dbo.hrm_job_site");
            DropForeignKey("dbo.IcmAsset", "AssetStatusId", "dbo.IcmMasLookUp");
            DropForeignKey("dbo.IcmAsset", "ResGrpId", "dbo.IcmMasResourceGroup");
            DropForeignKey("dbo.IcmAsset", "ResSubGrpId", "dbo.IcmMasResourceSubGroup");
            DropForeignKey("dbo.IcmAsset", "UnitId", "dbo.IcmMasUnit");
            DropForeignKey("dbo.IcmAsset", "WhId", "dbo.IcmWarehouse");
            DropForeignKey("dbo.IcmAsset", "AssetTypeId", "dbo.IcmMasResourceType");
            DropForeignKey("dbo.IcmAsset", "ResItemId", "dbo.IcmMasResourceItem");
            DropForeignKey("dbo.IcmAsset", "ProjExpId", "dbo.bgm_project_expense");
            DropForeignKey("dbo.IcmAsset", "ReceiveDtlId", "dbo.IcmReceiveDtl");
            DropForeignKey("dbo.IcmReceiveDtl", "DepreciationTypeId", "dbo.IcmMasDepreciationType");
            DropForeignKey("dbo.IcmReceiveDtl", "ProjExpId", "dbo.bgm_project_expense");
            DropForeignKey("dbo.IcmReceiveDtl", "UnitId", "dbo.IcmMasUnit");
            DropForeignKey("dbo.IcmReceiveDtl", "ResourceTypeId", "dbo.IcmMasResourceType");
            DropForeignKey("dbo.IcmReceiveDtl", "ResourceItemId", "dbo.IcmMasResourceItem");
            DropForeignKey("dbo.IcmReceiveDtl", "ReceiveId", "dbo.IcmReceive");
            DropForeignKey("dbo.IcmWarehousePermit", "JobSiteId", "dbo.hrm_job_site");
            DropForeignKey("dbo.IcmWarehousePermit", "WhId", "dbo.IcmWarehouse");
            DropForeignKey("dbo.IcmReceive", "ReceiveTypeId", "dbo.IcmMasLookUp");
            DropForeignKey("dbo.IcmReceive", "DnId", "dbo.IcmDeliveryNote");
            DropForeignKey("dbo.IcmReceive", "WhId", "dbo.IcmWarehouse");
            DropForeignKey("dbo.IcmMasContactPartner", "ConId", "dbo.IcmMasContact");
            DropForeignKey("dbo.IcmMasContactBank", "BranchId", "dbo.FimMasBankBranch");
            DropForeignKey("dbo.IcmMasContactBank", "ConId", "dbo.IcmMasContact");
            DropForeignKey("dbo.IcmMasPurchaseRegularMapping", "BgSourceId", "dbo.bgm_mas_source");
            DropForeignKey("dbo.IcmMasPurchaseRegularMapping", "RegularId", "dbo.IcmMasPurchaseRegular");
            DropTable("dbo.IcmRequestParcelDtl");
            DropTable("dbo.IcmRequestParcel");
            DropTable("dbo.IcmOfficeSuppliesLot");
            DropTable("dbo.IcmAssetWarranty");
            DropTable("dbo.IcmAsset");
            DropTable("dbo.IcmReceiveDtl");
            DropTable("dbo.IcmWarehousePermit");
            DropTable("dbo.IcmReceive");
            DropTable("dbo.IcmMasContactPartner");
            DropTable("dbo.IcmMasContactBank");
            DropTable("dbo.IcmMasPurchaseRegularMapping");
        }
    }
}
